public class ApproversConfigurationVF
{
    public Configuration__c[] configurationList = null; //stores the Approvers list
    
    public List<Configuration__c> displayApproverDetails {get; set;}
    public Boolean remAppFlag {get; set;}//Flag for Remove Approver button
    public Integer displayXRows {get; set;}//except final approver, display all rows in the first table
    
    String user = 'select';//SelectList variable
    private Configuration__c con = null;
    private Boolean saveStatus {get; set;}
    private Integer approverCount {get; set;}   
    
    String[] approverLevelList = new String[]{'Manager', 'Second Level Manager', 'Third Level Manager', 'Fourth Level Manager', 'Fifth Level Manager', 'Sixth Level Manager', 'Seventh Level Manager', 'Eigth Level Manager', 'Ninth Level Manager', 'Tenth Level Manager', 'Eleventh Level Manager', 'Twelfth Level Manager', 'Thirteenth Level Manager', 'Fourteenth Level Manager', 'Fifteenth Level Manager','Final Approver'}; 
    String[] approverLabelList = new String[]{'Approver 1', 'Approver 2', 'Approver 3', 'Approver 4', 'Approver 5', 'Approver 6', 'Approver 7', 'Approver 8', 'Approver 9', 'Approver 10', 'Approver 11', 'Approver 12', 'Approver 13', 'Approver 14', 'Approver 15', 'Final Approver'};
    
    public ApproversConfigurationVF()
    {
        configurationList = new List<Configuration__c>();
        displayApproverDetails = new List<Configuration__c>();
        for(Configuration__c[] cList: [select Id, Approver__c, Approver_Level__c, Response_Window__c, IsFinalApprover__c from Configuration__c])
            for(Configuration__c c: cList)
                configurationList.add(c);
                
        approverCount = 1;
        remAppFlag = true;//initially Remove Approver button is disabled
        //if no approvers were saved earlier
        if(configurationList.size() == 0)
        {
            getInitialDisplayDetails();
        }
        else
        {
            saveStatus = true;
            approverCount = configurationList.size() - 1;
            if(approverCount > 1)
                remAppFlag = false;
            
            displayApproverDetails = configurationList;
        }
        
        displayXRows = displayApproverDetails.size() - 1;
        setUser(displayApproverDetails[displayXRows].Approver_Level__c);
    }
    
    public List<SelectOption> getUserList()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('select','Select'));

        for(User[] userList : [select Id, Name from User where IsActive = true])
        {
            for(User user: userList)
            {
                if(options.size() <= 1000)
                {
                    options.add(new SelectOption(user.Id, user.Name));
                }
                else
                    break;
            }
        }
        return options;
    }
    
    public String getUser()
    {
        return user;
    }
    
    public void setUser(String user) 
    {
        this.user = user;
    }   
    
    private void getInitialDisplayDetails()
    {
        con = new Configuration__c(Approver__c = approverLabelList[0], Approver_Level__c = approverLevelList[0], Response_Window__c = 5, IsFinalApprover__c = 'No');
        displayApproverDetails.add(con);
        con = new Configuration__c(Approver__c =  approverLabelList[approverLevelList.size() - 1], Approver_Level__c = 'select', Response_Window__c = 0, IsFinalApprover__c = 'Yes');
        displayApproverDetails.add(con);
    }
    
    //adds new row to the table
    public PageReference addApprover()
    {
        if(approverCount < 15)
        {
            con = new Configuration__c(Approver__c = approverLabelList[approverCount], Approver_Level__c = approverLevelList[approverCount], Response_Window__c = 5, IsFinalApprover__c = 'No');
            displayApproverDetails.add(displayApproverDetails.size() -1, con);
            approverCount++;
            if(approverCount == 2)
                remAppFlag = !remAppFlag;
            displayXRows = displayApproverDetails.size() - 1;
        }
        return null;
    }
    
    public PageReference removeApprover()
    {
        
        if(approverCount > 1)
        {
            displayApproverDetails.remove(displayApproverDetails.size() - 2);
            approverCount--;
            displayXRows = displayApproverDetails.size() - 1;
            if(approverCount == 1)
                remAppFlag = !remAppFlag;
        }
        return null;
    }
    
    public PageReference saveApproverConfiguration()
    {
        try
        {
            Configuration__c[] configList = [select id from Configuration__c];
            //delete existing Approvers.
            if(configList.size() > 0)
                delete configList;
            
            //update Final Approver record with the current selected value.    
            displayApproverDetails[displayXRows].Approver_Level__c = user;
            
            if(displayApproverDetails[displayXRows].Approver_Level__c == 'select')
                displayApproverDetails[displayXRows].Response_Window__c = 0;
                
            ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.info, 'Approvers saved successfully.');            
    
                
            if(saveStatus == null)
            {
                saveStatus = false;
                insert displayApproverDetails;
            }
            
            if(saveStatus)
            {
                Configuration__c[] newConfigList = new List<Configuration__c>();
                Configuration__c con = null;
                for(Configuration__c oldConfig: displayApproverDetails)
                {
                    con = new Configuration__c(Approver__c = oldConfig.Approver__c, Approver_Level__c = oldConfig.Approver_Level__c, Response_Window__c = oldConfig.Response_Window__c, IsFinalApprover__c = oldConfig.IsFinalApprover__c);
                    newConfigList.add(con);        
                }
                if(newConfigList.size() > 0)
                    insert newConfigList;
            }
            ApexPages.addMessage(msg);
            saveStatus = true;
        }
        catch(Exception e){ApexPages.addMessages(e);}
        return null;
    }
}//end of class